package com.wyx.producer.generalTest;

import com.wyx.pojo.User;
import com.wyx.utils.commonUtil.CUtil;
import com.wyx.utils.threadSave.ThreadUserInfo;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class General {
    @Test
    public void test1() {
        StringBuilder stringBuilder = new StringBuilder();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String format = dateFormat.format(date);
        stringBuilder.append(format);
        stringBuilder.append("0001");
//        20240805000288880000002012345678
        System.out.println(format);
    }

    public static void main(String[] args) {
        General general = new General();

        User user = new User();
        user.setId("111");
        ThreadUserInfo.set(user);
        String serialId = general.getSerialId("20240805000288880000002012345678", 321);
        System.out.println(serialId);
    }

    public String getSerialId(String maxSerialId, int currentBatchQuantity) {
        if (currentBatchQuantity <= 0 || currentBatchQuantity > 100000000) {
            throw new RuntimeException("批次数量过多，大概率是被攻击了");
        }

        String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
        StringBuilder stringBuilder = new StringBuilder(currentDate);
        if (maxSerialId == null || !maxSerialId.startsWith(currentDate)) {
            stringBuilder.append("0001");
        } else {
            int batchNumber = Integer.parseInt(maxSerialId.substring(8, 12)) + 1;
            stringBuilder.append(String.format("%04d", batchNumber));
        }

        stringBuilder.append(ThreadUserInfo.get().getId().equals("111") ? "8888" : "9999");
        stringBuilder.append(String.format("%08d", currentBatchQuantity));
        stringBuilder.append(CUtil.getUUID(), 0, 12);
        return stringBuilder.toString();
    }
}
